package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.gym.baselib.utils.CacheUtils
import com.ht.medicaltreatment.base.CacheKey
import com.ht.medicaltreatment.bean.QueryOrderBean
import com.ht.medicaltreatment.bean.SghyMarshall
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch


class GroupOrderViewModel : BaseViewModel() {
    fun checkId(
        uid: String,
        onSuccess: (MutableList<SghyMarshall>) -> Unit,
        onError: (String) -> Unit
    ) {
        viewModelScope.launch {
            val bean = mApi.groupOrderId(uid, "SGHY", "301医院")
            when (bean) {
                is ApiResult.Success -> orderList(bean.data.data, onSuccess, onError)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    fun orderList(
        orderId: Int,
        onSuccess: (MutableList<SghyMarshall>) -> Unit,
        onError: (String) -> Unit
    ) {
        CacheUtils.save(CacheKey.ORDER_ID, "$orderId")
        viewModelScope.launch {
            val bean = mApi.groupOrderList("$orderId")
            when (bean) {
                is ApiResult.Success -> onSuccess(bean.data.data.sghyMarshallList)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    fun queryOrder(
        currentPage: String,
        pageSize: String,
        projectId: String,
        orderName: String,
        checkSign:String,
        onSuccess:(MutableList<QueryOrderBean.RowsBean>)->Unit,
        onError: (String) -> Unit
    ) {

        viewModelScope.launch {
            val bean = mApi.groupQueryOrderAPI(currentPage,pageSize,projectId,checkSign,orderName)
            when (bean) {
                is ApiResult.Success -> onSuccess(bean.data.rows)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }

       /* EasyHttp.post(this)
            .api(GroupQueryOrderAPI(currentPage,pageSize,projectId,orderName))
            .request(object : OnHttpListener<QueryOrderBean>{
                override fun onHttpSuccess(bean: QueryOrderBean) {
                    onSuccess(bean.data.pageList)
                }

                override fun onHttpFail(error: Exception) {
                    ToastUtils.showShort(error.message)
                    ProgressUtils.dismiss()
                }
            })*/
    }
}